<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Models\SequenceModel;

class SequenceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '岗位职称';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        'index'  => '这里展示的是岗位职称，默认配置，请勿随意编辑修改',
        'show'   => '岗位职称详情',
        'edit'   => '请在此处编辑岗位职称',
        'create' => '请创建新的岗位职称',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $parents = SequenceModel::select(['sequence_id', 'name_zh'])->where('parent_id', '=', 0)->get()->toArray();
        $parents = array_column($parents, 'name_zh', 'sequence_id');

        $grid = new Grid(new SequenceModel);

        // 添加筛选条件
        $grid->model()->where('parent_id', '>', 0);
        $grid->model()->orderBy('order', 'asc');
        $grid->model()->orderBy('sequence_id', 'desc');

//        $grid->column('sequence_id', __('ID'))->width(60)->sortable();
        $grid->column('order', __('优先级'))->width(110)->sortable();
        $grid->column('parent_id', __('岗位类别'))->display(function ($parentId) use ($parents) {
            return isset($parents[$parentId]) ? $parents[$parentId] : '';
        });
        $grid->column('name_zh', __('岗位职称名称-中文'));
        $grid->column('name_en', __('岗位职称名称-英文'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->filter(function (Grid\Filter $filter) use ($parents) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name_zh', __('岗位职称名称-中文'));
            $filter->like('name_en', __('岗位职称名称-英文'));

            $filter->equal('parent_id', __('岗位类别'))->select($parents);
        });

        // 禁用导出数据按钮
        $grid->disableExport();

        // 设置样式
        \Encore\Admin\Admin::style('.column-__row_selector__ {width: 70px;} .column-__actions__ {width: 70px;}');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $parents = SequenceModel::select(['sequence_id', 'name_zh'])->where('parent_id', '=', 0)->get()->toArray();
        $parents = array_column($parents, null, 'sequence_id');

        $show = new Show(SequenceModel::findOrFail($id));

//        $show->field('sequence_id', __('ID'))->setWidth(2, 2);
        $show->field('order', __('优先级'))->setWidth(2, 2);
        $show->field('parent_id', __('岗位类别'))->as(function ($parentId) use ($parents) {
            return isset($parents[$parentId]) ? $parents[$parentId]['name_zh'] : '';
        })->setWidth(2, 2);
        $show->field('name_zh', __('岗位职称名称-中文'))->setWidth(2, 2);
        $show->field('name_en', __('岗位职称名称-英文'))->setWidth(2, 2);
        $show->field('created_at', __('创建时间'))->setWidth(2, 2);
        $show->field('updated_at', __('更新时间'))->setWidth(2, 2);

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SequenceModel);

        $form->number('order', __('优先级'))->min(1)->setWidth(2, 2)->default(1)->rules('required|min:1')->required();

        $parents = SequenceModel::select(['sequence_id', 'name_zh'])->where('parent_id', '=', 0)->get()->toArray();
        $parents = array_column($parents, 'name_zh', 'sequence_id');
        $form->select('parent_id', __('岗位类别'))->options($parents)->rules('required|min:1')->setWidth(2, 2)->required();

        $form->text('name_zh', __('岗位职称名称-中文'))->rules('required|min:1|max:40')->maxlength(40)
            ->placeholder('输入岗位职称名称-中文，字符长度限制在40字符内')->setWidth(2, 2)->required();
        $form->text('name_en', __('岗位职称名称-英文'))->rules('required|min:1|max:80')->maxlength(80)
            ->placeholder('输入岗位职称名称-英文，字符长度限制在80字符内')->setWidth(2, 2)->required();

        return $form;
    }

    /**
     * 获取职称下拉列表
     *
     * @return array
     */
    public function sequenceSelect()
    {
        $pids = \Request::input('q', '');
        if (empty($pids)) {
            return [];
        }
        $pids = explode(',', $pids);
        $pidsFilter = [];
        foreach ($pids as $pid) {
            if (!empty($pid) && $pid != 'undefined' && $pid != 'null') {
                $pidsFilter[] = $pid;
            }
        }

        if (empty($pidsFilter)) {
            $pidsFilter = [1, 2];
        }

        $data = [];
        $list = SequenceModel::getSequenceList(['parent_id' => $pidsFilter]);
        foreach ($list as $item) {
            $data[] = [
                'id'   => $item['sequence_id'],
                'text' => $item['name_zh']
            ];
        }

        return $data;
    }
}
